package com.tuojiahr.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.Preparable;
import com.tuojiahr.common.BaseActionCommon;
import com.tuojiahr.domain.TuoJiaHrUser;
import com.tuojiahr.global.Constants;
import com.tuojiahr.pagemodel.PageModel;
import com.tuojiahr.service.EmployeeService;
import com.tuojiahr.tools.StringUtil;

/**
 * 说明：
 *
 * @author wang.haibo
 * @date 2012-11-13 上午12:34:30
 */

public class EmployeeAction extends BaseActionCommon implements Preparable {

	private static final long serialVersionUID = 1L;
	
	private EmployeeService employeeService;
	
	private String message = "出错啦！！！";
	
	private PageModel pageModel;
	private String keyWord; 
	private String assign;
	private String url = "adminmgr/emp_getEmplyeeNoDeleteList";
	
	private TuoJiaHrUser user;

	@Override
	public void prepare() throws Exception {
		user = getCurrUser();
	}
	
	/**
	 * 获取未删除的报名人员
	 * @return
	 */
	public String getEmplyeeNoDeleteList() {
		if(null != user) {
			if(StringUtil.isBlank(assign)) {
				assign = "n";
			} 
			try {
				pageModel = employeeService.getEmployeeNoDeleteList(keyWord, assign);
				//是否主页显示
				Map<String, String> selectAssign = new HashMap<String, String>();
				selectAssign.put("y", "已分配");
				selectAssign.put("n", "未分配");
				request.setAttribute("selectAssign", selectAssign);
				return "nodelete_emp_list";
			} catch (Exception e) {
				Constants.ExceptionLog.error("##EmployeeAction-----getEmplyeeNoDeleteList方法查询出错", e);
			}
		}
		return "msg";
	}
	
	/**
	 * 更新分配状态
	 * @return
	 * @throws IOException 
	 */
	public void updateAssign() throws IOException {
		String id = request.getParameter("id");
		String result = "fault";
		if(null != user && StringUtil.isNotBlank(id)) {
			try {
				employeeService.updateEmployee("y", Long.valueOf(id), user);
				result = "success";
			} catch (NumberFormatException e) {
				result = "error";
				Constants.ExceptionLog.error("##EmployeeAction-----updateAssign方法更新出错", e);
			}
		}
		response.getWriter().print(result);
		response.getWriter().close();
		
	}
	
	/**
	 * 标识为已删除
	 * @throws IOException
	 */
	public void deleteEmployee() throws IOException {
		String result = "fault";
		String ids = request.getParameter("ids");
		if(null != user && StringUtil.isNotBlank(ids)) {
			try {
				employeeService.deleteEmployee(ids, user);
				result = "success";
			} catch (Exception e) {
				result = "error";
				Constants.ExceptionLog.error("##EmployeeAction-----deleteEmployee方法更新出错", e);
			}
		}
		response.getWriter().print(result);
		response.getWriter().close();
	}
	
	/**
	 * 加载已删除的报名人员
	 * @return
	 */
	public String getEmployeeIsDeleteList() {
		url = "adminmgr/emp_getEmployeeIsDeleteList";
		if(null != user) {
			try {
				pageModel = employeeService.getEmployeeIsDeleteList(keyWord);
				return "isdelete_emp_list";
			} catch (Exception e) {
				Constants.ExceptionLog.error("##EmployeeAction-----getEmployeeIsDeleteList方法查询出错", e);
			}
		}
		return "msg";
	}
	
	/**
	 * 彻底删除
	 * @throws IOException
	 */
	public void deleteTrueEmployee() throws IOException {
		String result = "fault";
		String ids = request.getParameter("ids");
		if(null != user && StringUtil.isNotBlank(ids)) {
			try {
				employeeService.deleteTrueEmployee(ids);
				result = "success";
			} catch (Exception e) {
				result = "error";
				Constants.ExceptionLog.error("##EmployeeAction-----deleteTrueEmployee方法删除出错", e);
			}
		}
		response.getWriter().print(result);
		response.getWriter().close();
	}

	public PageModel getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel pageModel) {
		this.pageModel = pageModel;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getAssign() {
		return assign;
	}

	public void setAssign(String assign) {
		this.assign = assign;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
